package dao;

import java.util.ArrayList;
import javax.sql.DataSource;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import dto.CompanyDto;

public class CompanyDaoImpl implements CompanyDao {
	private JdbcTemplate template;

	public void setDataSource(DataSource dataSource) {
		this.template = new JdbcTemplate(dataSource);
	}
	
	private static final String INSERT_FILE_INSERT = "INSERT INTO company(s_date, e_date, name, kind, email, link) "
			+ " VALUES(?,?,?,?,?,?)";

	@Override
	public int insertCompany(CompanyDto bean) {
		// TODO Auto-generated method stub
		return this.template.update(INSERT_FILE_INSERT,
				new Object[] { bean.getS_date(), bean.getE_date(), bean.getName(),
						bean.getKind(), bean.getEmail(), bean.getLink() });
	}

	//private static final String SELECT_FILE_LIST = "SELECT * FROM file WHERE file_key = ?";
	private static final String SELECT_FILE_LIST = "SELECT pr_key, s_date, e_date, name, kind, email, link FROM company";
	
	@Override
	public ArrayList<CompanyDto> companyList(String type) {
		// TODO Auto-generated method stub
		RowMapper<CompanyDto> mapper = new BeanPropertyRowMapper<CompanyDto>(CompanyDto.class);
		ArrayList<CompanyDto> query = (ArrayList<CompanyDto>) template.query(SELECT_FILE_LIST, mapper);
		System.out.println("리스트 쿼리 내용 = " + query);
		return query;
	}

	private static final String DELECT_FILE_REMOVE = "DELETE FROM company WHERE pr_key = ?";

	@Override
	public int removeCompany(CompanyDto bean) {
		// TODO Auto-generated method stub
		return this.template.update(DELECT_FILE_REMOVE, bean.getPr_key());
	}
}
